<template>
  <div class="login-panel-account">
    <el-tab-pane name="phone">
      <!-- Tab-pane 插槽​
          插槽名	说明
          default	Tab-pane 的内容
          label	Tab-pane 的标题内容 -->
      <template #label>
        <div class="label">
          <el-icon><Iphone /></el-icon>
          <span class="text">手机登录</span>
        </div>
      </template>
      <div>
        <el-form label-width="70" :model="form" size="large" :rules="PhoneRules">
          <el-form-item label="帐号" prop="phone">
            <el-input v-model="form.phone" />
          </el-form-item>
          <el-form-item label="验证码" prop="code">
            <div class="verify-code">
              <el-input show-password v-model="form.code" />
              <span>
                <el-button class="get-btn" type="primary">获取验证码</el-button>
              </span>
            </div>
          </el-form-item>
        </el-form>
      </div>
    </el-tab-pane>
  </div>
</template>

<script setup lang="ts">
import { reactive, ref } from 'vue'
import type { FormRules, ElForm } from 'element-plus'
import { ElMessage } from 'element-plus'
import useLoginStore from '@/storage/login/login.ts'

const PhoneRules: FormRules = {
  // 手机
  phone: [
    { required: true, message: '请输入手机号', trigger: 'blur' }, // blur 失去焦点触发验证
  ],
  code: [{ required: true, message: '请输入验证码', trigger: 'blur' }],
}

const form = reactive({
  code: '',
  phone: '',
})

const formRef = ref<InstanceType<typeof ElForm>>()
// pinal
const loginStorage = useLoginStore()

// 接收传过来的参数 ：？ = 非必填
function LoginActionByPhone(isRemPwd: boolean) {
  formRef.value?.validate((vaild, field) => {
    if (vaild) {
      console.log('验证成功')

      let code = form.code
      let phone = form.phone

      // const formData = new FormData()
      // formData.append('user_name', user_name)
      // formData.append('password', password)

      // 通过pinal 发起请求 并直接保存状态！
      // loginRequest(formData).then((res) => {
      //   console.log("=============")
      //   console.log(res)
      // })

      // 方式二 pinal 手机号直接登陆 没有记住密码这些东西
      // loginStorage.UserLoginAction({ code, phone }) // 这里重新做登陆
      console.log('token:', loginStorage.getLoginInfo.token)
    } else {
      for (const key in field) {
        if (field.hasOwnProperty(key)) {
          const errors = field[key]
          for (const error of errors) {
            const message = error.message
            console.log(message)
            ElMessage.error(message)
          }
        }
      }
    }
  })
}

// 暴露出子组件的函数
defineExpose({
  LoginActionByPhone,
})
</script>

<style scoped>
.verify-code {
  display: flex;
  .get-btn {
    margin-left: 8px;
  }
}
</style>
